#!/bin/sh

[ -f /tmp/.android ] || exit 0

if [ -e /etc/config/minidlna ]; then
    /sbin/uci set minidlna.@minidlna[0].interface='br-lan,eth0,wlan0'
    /sbin/uci commit minidlna
fi

[ -f /etc/config/samba -a ! -L /etc/config/samba ] && ln -s samba /etc/config/samba4
[ -f /etc/config/samba4 -a ! -L /etc/config/samba4 ] && ln -s samba4 /etc/config/samba

# just procded init script should be set enabled flag by init status.
NO_ST_SERVICES="samba samba4"

for service in $NO_ST_SERVICES; do
    init=$service
    [ "$init" == "vsftpd" -a -f /etc/init.d/vsftpd-uci ] && init="vsftpd-uci"
    if [ -e /etc/init.d/$init ]; then
        [ -e /etc/config/$service ] || touch /etc/config/$service
        SECTION=$service
        [ "$SECTION" == "samba4" ] && SECTION="samba"
        [ "$SECTION" == "afpd" ] && SECTION="netatalk"
        uci rename $service.@$SECTION[0]='config' >>/dev/null 2>&1
        uci set $service.config=$SECTION
        if /etc/init.d/$init enabled; then
            /sbin/uci set $service.config.enabled=1
        else
            /sbin/uci set $service.config.enabled=0
        fi
        /sbin/uci commit $service
    fi
done

for service in vsftpd minidlna forked-daapd transmission netatalk; do
    if [ -e /etc/init.d/$service ]; then
        [ -e /etc/config/$service ] || touch /etc/config/$service
        if ! uci get $service.config.enabled >>/dev/null 2>&1; then
            uci rename $service.@$service[0]='config' >>/dev/null 2>&1
            uci set $service.config=$service
            uci set $service.config.enabled=0
            uci commit $service
        fi
    fi
done

exit 0
